<template>
  <el-main>
    <el-table :data="employeeList">
      <el-table-column prop="number" label="工号" width="220" align="center"></el-table-column>
      <el-table-column prop="name" label="姓名" width="120" align="center"></el-table-column>
      <el-table-column prop="sex" label="性别" width="120" align="center"></el-table-column>
      <el-table-column prop="age" label="年龄" width="80" align="center"></el-table-column>
      <el-table-column prop="yearIn" label="入职时间" width="180" align="center"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="deleteEmployee(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-main>
</template>

<script>
import { postRequest } from "../utils/api";
import { postRequestByParams } from "../utils/api";

export default {
  data() {
    return {
      employee: {
        number: "",
        name: "",
        sex: "",
        age: "",
        yearIn: ""
      },
      employeeList: []
    };
  },
  methods: {
    deleteEmployee: function(row) {
      var _this = this;
      this.$confirm("此操作将永久该员工, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          postRequestByParams("/employee/delete-one-employee", {
            number: row.number
          }).then(resp => {
            if (resp.status == 200) {
              var json = resp.data;
              this.$alert(json.data);
            } else {
              this.$alert("未知错误！");
            }
            _this.getAllEmployee();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    getAllEmployee: function() {
      var _this = this;
      postRequest("/employee/get-all-employee").then(resp => {
        if (resp.status == 200) {
          var json = resp.data;
          _this.employeeList = json.data;
        }
      });
    }
  },
  mounted() {
    this.getAllEmployee();
  }
};
</script>
    
<style>
</style>